<?php

namespace App\Manage;


use App\Models\Course;
use App\Service\Base\Manage;


class CourseCycleManage extends Manage
{

    // 课程周期
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseCycle';
    }

    // 列表
    // 添加
    // 修改

    // 删除
    public function onDelete()
    {
        $result = $this->toDelete();

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($keyword){
            $bang = false; // 设置不在缓存
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $c_id = $opt->getData("course_id", null, 'intval');
        if(empty($c_id)){
            return $this->_returnFail('请选择课程！');
        }
        // 获取课程信息
        $cInfo = (new Course())->isCache(true)->oneFind(['id' => $c_id]);
        if(empty($cInfo)){
            return $this->_returnFail('选择的课程不存在！');
        }

        $data = [
            //'id'         => $opt->getData("id", null, "trim"),
            'uid'        => $cInfo['uid'],
            'course_id'  => $c_id,
            'cycle'      => $opt->getData("cycle", null, "trim"),
            'number'     => $opt->getData("number", null, "trim"),
            'start_time' => $opt->getData("start_time", null, "strtotime"),
            'end_time'   => $opt->getData("end_time", null, "strtotime"),
            //'ctime'      => $opt->getData("ctime", null, "trim"),
            //'mtime'      => $opt->getData("mtime", null, "trim"),
        ];

        // 最终生成的时间戳 必须在一天之内
        // 但直接生成的时间戳 默认是当天 所以要减去当天的开始时间戳
        // 获取今天日期的开始时间戳
        $timestamp = strtotime("today");
        if($data['start_time'] > 86400){
            $data['start_time'] = $data['start_time'] - $timestamp;
        }
        if($data['start_time'] < -86400){
            $data['start_time'] = $data['start_time'] + $timestamp;
        }
        if($data['end_time']){
            if($data['end_time'] > 86400){
                $data['end_time'] = $data['end_time'] - $timestamp;
            }
            if($data['end_time'] < -86400){
                $data['end_time'] = $data['end_time'] + $timestamp;
            }
        }

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $c_id = $opt->getData("course_id", null, 'intval');
        if(empty($c_id)){
            return $this->_returnFail('请选择课程！');
        }

        $data = [
            //'id'         => $opt->getData("id", null, "trim"),
            //'uid'        => $opt->getData("uid", null, 'intval'),
            'course_id'  => $c_id,
            'cycle'      => $opt->getData("cycle", null, "trim"),
            'number'     => $opt->getData("number", null, "trim"),
            'start_time' => $opt->getData("start_time", null, "strtotime"),
            'end_time'   => $opt->getData("end_time", null, "strtotime"),
            //'ctime'      => $opt->getData("ctime", null, "trim"),
            //'mtime'      => $opt->getData("mtime", null, "trim"),
        ];

        // 最终生成的时间戳 必须在一天之内
        // 但直接生成的时间戳 默认是当天 所以要减去当天的开始时间戳
        // 获取今天日期的开始时间戳
        $timestamp = strtotime("today");
        if($data['start_time'] > 86400){
            $data['start_time'] = $data['start_time'] - $timestamp;
        }
        if($data['start_time'] < -86400){
            $data['start_time'] = $data['start_time'] + $timestamp;
        }
        if($data['end_time']){
            if($data['end_time'] > 86400){
                $data['end_time'] = $data['end_time'] - $timestamp;
            }
            if($data['end_time'] < -86400){
                $data['end_time'] = $data['end_time'] + $timestamp;
            }
        }

        return $data;
    }

}


